﻿/*
  In App.xaml:
  <Application.Resources>
      <vm:ViewModelLocatorTemplate xmlns:vm="clr-namespace:ERTestingGui.ViewModel"
                                   x:Key="Locator" />
  </Application.Resources>
  
  In the View:
  DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
  
  OR (WPF only):
  
  xmlns:vm="clr-namespace:ERTestingGui.ViewModel"
  DataContext="{Binding Source={x:Static vm:ViewModelLocatorTemplate.ViewModelNameStatic}}"
*/

using System;
using System.Collections.Generic;
using ERTesting.Model.Result;
using GalaSoft.MvvmLight;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System.Configuration;
namespace ERTesting.Gui.ViewModel
{
    /// <summary>
    /// This class contains static references to all the view models in the
    /// application and provides an entry point for the bindings.
    /// <para>
    /// Use the <strong>mvvmlocatorproperty</strong> snippet to add ViewModels
    /// to this locator.
    /// </para>
    /// <para>
    /// In Silverlight and WPF, place the ViewModelLocatorTemplate in the App.xaml resources:
    /// </para>
    /// <code>
    /// &lt;Application.Resources&gt;
    ///     &lt;vm:ViewModelLocatorTemplate xmlns:vm="clr-namespace:ERTestingGui.ViewModel"
    ///                                  x:Key="Locator" /&gt;
    /// &lt;/Application.Resources&gt;
    /// </code>
    /// <para>
    /// Then use:
    /// </para>
    /// <code>
    /// DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
    /// </code>
    /// <para>
    /// You can also use Blend to do all this with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// <para>
    /// In <strong>*WPF only*</strong> (and if databinding in Blend is not relevant), you can delete
    /// the Main property and bind to the ViewModelNameStatic property instead:
    /// </para>
    /// <code>
    /// xmlns:vm="clr-namespace:ERTestingGui.ViewModel"
    /// DataContext="{Binding Source={x:Static vm:ViewModelLocatorTemplate.ViewModelNameStatic}}"
    /// </code>
    /// </summary>
    public class ViewModelLocator
    {
        
        #region Private Members

        private readonly IUnityContainer _container;
        private static readonly ViewModelLocator _instance = new ViewModelLocator();

        #endregion Private Members

        public static ViewModelLocator Instance { get
        {
     
            return _instance;
        } }

       
       #region Constructors


        /// <summary>
        /// Initializes a new instance of the ViewModelLocator class.
        /// </summary>
        private ViewModelLocator()
        {
            if (ViewModelBase.IsInDesignModeStatic)
            {
                    // Create design time view models
            }
            else
            {
                 _container = new UnityContainer();
                UnityConfigurationSection section
    = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
                section.Configure(_container, "containerFileStore");

            }

        }

        #endregion Constructors


        /// <summary>
        /// Gets the Main property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public MainViewModel Main
        {
            get
            {
                return _container.Resolve<MainViewModel>();
            }
        }

        public T GetViewModel<T>(Dictionary<String, object> paramOverrides)
        {
            ParameterOverrides propertyOverrides = new ParameterOverrides();
            foreach (KeyValuePair<string, object> paramOverride in paramOverrides)
            {
                propertyOverrides.Add(paramOverride.Key, paramOverride.Value);
                
            }
            return _container.Resolve<T>(propertyOverrides);
        }

        /// <summary>
        /// Cleans up all the resources.
        /// </summary>
        public void Cleanup()
        {
            //ClearMain();
        }

        public T GetViewModel<T>(string firstParameterName, object parameterValue)
        {
            return GetViewModel<T>(new Dictionary<string, object>(){{firstParameterName,parameterValue}});
        }
    }
}